package com.ugrow.ugrowcommunity.vm.home;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.Nullable;
import androidx.lifecycle.ViewModelProviders;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.ugrow.ugrowcommunity.activity.campus.CampusCardActivity;
import com.ugrow.ugrowcommunity.activity.dormitoryselection.DormitorySelectionActivity;
import com.ugrow.ugrowcommunity.activity.professional.ProfessionalActivity;
import com.ugrow.ugrowcommunity.activity.warranty.WarrantyActivity;
import com.ugrow.ugrowcommunity.databinding.HomeFragmentBinding;
import com.ugrow.ugrowcommunity.util.InitBean;
import com.ugrow.ugrowcommunity.vm.base.BaseFragment;

import java.util.ArrayList;

public class HomeFragment extends BaseFragment<HomeFragmentBinding,HomeViewModel> {

    public static HomeFragment newInstance() {
        return new HomeFragment();
    }


    @Override
    protected HomeFragmentBinding setContentView(LayoutInflater inflater, @Nullable ViewGroup container) {
        return HomeFragmentBinding.inflate(inflater, container, false);
    }

    @Override
    protected HomeViewModel setViewModel() {
        return ViewModelProviders.of(this).get(HomeViewModel.class);
    }

    @Override
    protected void onVisible() {
        binding.xBanner.startAutoPlay();
    }

    @Override
    protected void onInvisible() {
        super.onInvisible();
        binding.xBanner.stopAutoPlay();
    }

    @Override
    protected void onFirstLoad() {

    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initBanner();

        initInfoList();
        binding.refreshLayout.setOnRefreshListener(refreshLayout -> {
            //下拉刷新
            Log.d("TAG", "initListener: 正在刷新");


            refreshLayout.finishRefresh();
        });
        binding.refreshLayout.setOnLoadMoreListener(refreshLayout -> {
            //上拉加载
            Log.d("TAG", "initListener: 正在加载");

            refreshLayout.finishLoadMore();
        });

        //页面跳转
        binding.warranty.setOnClickListener(v -> startActivity(new Intent(requireContext(), WarrantyActivity.class)));
        //宿舍
        binding.studentInformation.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(), DormitorySelectionActivity.class));
            }
        });
        //校园卡
        binding.courseSelection.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(), CampusCardActivity.class));
            }
        });
        //专业方向选择
        binding.ScoreQuery.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(), ProfessionalActivity.class));
            }
        });

    }

    private void initInfoList() {
        LinearLayoutManager layout = new LinearLayoutManager(requireContext());
        layout.setOrientation(LinearLayoutManager.VERTICAL);
        binding.recyclerview.setLayoutManager(layout);
        binding.recyclerview.setAdapter(new InfoAdapter(InitBean.getHomeList()));
    }


    private void initBanner() {
        ArrayList<String> img_list = new ArrayList<>();
        img_list.add("file:///android_asset/banner_1.jpg");
        img_list.add("file:///android_asset/banner_2.jpg");
        img_list.add("file:///android_asset/banner_3.jpg");
        img_list.add("file:///android_asset/banner_4.jpg");
        img_list.add("file:///android_asset/banner_5.jpg");
        binding.xBanner.setData(img_list, null);
        binding.xBanner.loadImage((banner, model, view, position) -> {
            Glide.with(requireActivity())
                    .load(img_list.get(position))
                    .apply(RequestOptions.bitmapTransform(new GlideRoundTransform(10)))
                    .into((ImageView) view);
        });
    }


}